<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>排序算法之选择排序</title>
  </head>
  <body>
    <script type="text/javascript">
      function swap(arr, i, j) {
        let temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
      }

      function selectSort(arr) {
        for (let i = 0; i < arr.length - 1; ++i) {
          let min = i;
          for (let j = i + 1; j < arr.length; ++j) {
            if (arr[j] < arr[min]) {
              min = j;
            }
          }
          swap(arr, i, min);
        }
      }

      const arr = [10, 6, 3, 4, 8, 1, 2];
      selectSort(arr);
      console.log(arr);
    </script>
  </body>
</html>
